package com.zte.warehouse.control;


import com.zte.market.model.MarketOrderCondition;
import com.zte.market.model.MarketOrders;
import com.zte.sys.login.model.LUsers;
import com.zte.util.OperatorLog;
import com.zte.util.PageModel;
import com.zte.util.ResultMessage;
import com.zte.warehouse.model.*;
import com.zte.warehouse.service.IInWarehouseService;
import com.zte.warehouse.service.IOutWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 仓库管理模块
 * <p>
 * 仓库管理模块使用者：财务部门经理  job_id=6
 * 创建仓库权限：财务部门经理
 */


@Controller
@RequestMapping("/OutWarehouse")
public class OutWarehouseAction {
    @Autowired
    private IOutWarehouseService iOutWarehouseService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/OutWarehousePage")
    @ResponseBody
    @OperatorLog(operate="多条件查询出库分页", module="出库管理")
    public ResultMessage getOutWarehousesPage(PageModel<OutWarehose> pageModel, OutWarehouseCondition conn) {
        System.out.println(conn);

        //1.身份验证：判断用户jobID, 财务部门经理看全部,非财务部门经理看自己
        LUsers User = (LUsers) redisTemplate.opsForValue().get(conn.getUserID() + "");
        ResultMessage msg = new ResultMessage();
        if (User != null) {
            if (User.getJobId() == 6l||User.getJobId() == 1l) {//财务部门经理、总经理
                conn.setUserID(null);
         // System.out.println("JobId:"+User.getJobId());
            }
            System.out.println("NowUser:" + User);
        }
        PageModel<OutWarehose> OutWarehousePageModel = iOutWarehouseService.queryOutWarehoseListByCondition(pageModel, conn);
        msg.setStatus(200);
        msg.setMsg("查询成功！");
        msg.setData(OutWarehousePageModel);

        return msg;
    }

    //添加出库信息
    @RequestMapping("/OutWarehouseAdd")
    @ResponseBody
    @OperatorLog(operate="添加出库信息，更新订单表出库状态", module="出库管理")
    public ResultMessage addInWarehouse(OutWarehoseAdd outWarehoseAdd) {

        outWarehoseAdd.setState("1");//默认未发货
        System.out.println("outWarehoseAdd"+outWarehoseAdd);
        int i = iOutWarehouseService.OutWarehoseAdd(outWarehoseAdd);
        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        msg.setMsg("添加出库成功！");
        msg.setData(i);
        return msg;
    }
    //验证订单审核状态
    @RequestMapping("/queryOrderState")
    @ResponseBody
    public ResultMessage queryPurchaseState(String indent) {
        MarketOrders order = iOutWarehouseService.queryOrderState(indent);
        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        msg.setMsg("查询状态成功！");
        msg.setData(order);
        return msg;
    }

    //test
//    @RequestMapping("/test")
//    @ResponseBody
//    public ResultMessage test(MarketOrderCondition con) {
//        System.out.println(con);
//        List<MarketOrders> marketOrders = iOutWarehouseService.testFunction(con);
//
//        ResultMessage msg = new ResultMessage();
//        msg.setStatus(200);
//        msg.setMsg("test！");
//        msg.setData(marketOrders);
//        return msg;
//    }
    //更新出库状态
    @RequestMapping("/updateOutWarehouseState")
    @ResponseBody
    @OperatorLog(operate="更新出库状态，并更新仓库金额、库存商品", module="出库管理")
    public ResultMessage updateInWarehouseState(OutWarehouseUpdateCondition con) {
        System.out.println(con);
        int i = iOutWarehouseService.updateOutWarehose(con);
        ResultMessage msg = new ResultMessage();
        msg.setStatus(200);
        if (i>0)
        msg.setMsg("更新出库状态成功！");
        if(i==-1)
            msg.setMsg("更新失败");
        if(i==-2)
            msg.setMsg("库存不足");
        msg.setData(i);
        return msg;
    }




}
